package com.ljf.admin.controller;


import com.ljf.admin.POJO.SysDept;
import com.ljf.admin.service.SysDeptService;
import com.ljf.admin.service.SysUserDeptService;
import com.ljf.framework.result.JsonResult;
import com.ljf.framework.util.tree.TreeObject;
import com.ljf.framework.util.tree.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Auther: lijinfeng
 * @Date: 2022/5/11
 * @Description 描述:
 */
@RestController
@RequestMapping("/system/dept")
public class DeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysUserDeptService sysUserDeptService;

    @RequestMapping("/tree")
    public JsonResult getMenuTree() {
        List<SysDept> list = sysDeptService.list();
        List<TreeObject> tree = TreeUtil.getTree(list);
        return JsonResult.success(tree);
    }


    @PostMapping("/{parentId}")
    public JsonResult addDpet(@PathVariable Long parentId, @RequestBody SysDept sysDept) {
        System.out.println(sysDept.toString());
        sysDept.setParentId(parentId);
        // todo 获取最大顺序号 计算得出当前菜单顺序号 并设置
        sysDeptService.save(sysDept);
        return JsonResult.success();
    }

    @DeleteMapping("/{deptId}")
    public JsonResult deleteDept(@PathVariable Long deptId) {
        sysDeptService.deleteDept(deptId);
        return JsonResult.success();

    }

    @PutMapping
    public JsonResult updateDept(@RequestBody SysDept sysDept){
        boolean save = sysDeptService.saveOrUpdate(sysDept);
        if(save)
            return JsonResult.success("修改部门信息成功");
        return JsonResult.error("修改部门信息失败");
    }

    @GetMapping("/user")
    public JsonResult getDeptUser(){
        List<Map<String, String>> list = sysDeptService.getDeptUser();
        System.out.println(list.toString());
        return JsonResult.success(list);
    }

    @PutMapping("/user/{deptId}")
    public JsonResult updateDeptUser(@PathVariable Long deptId , @RequestBody Long[] userIds){
        sysUserDeptService.updateDeptUser(deptId,userIds);
        return JsonResult.success();
    }
}
